import dayjs from "dayjs";
import { numberC } from "@/util/common";
import { addC } from "@/util/computePtrcision.ts";

/**
 *@Author: QWP
 *@Description: 根据时间区间生成数据
 *@Date 2024-09-11 18:51
 */
export const eachCreateDataByDate = (data: any[], params: { key: string; key1: string }): any[] => {
  let results: any[] = [];
  const { key = "", key1 = "" } = params;
  for (let i = 0; i < data.length; i++) {
    const item = data[i];
    if (item[key] == item[key1]) {
      results.push(item);
    } else {
      let gapDay = dayjs(item[key]).diff(dayjs(item[key]), "day") + 2;
      for (let i = 0; i < gapDay; i++) {
        let tItem = { ...item };
        tItem[key] = dayjs(item[key]).add(i, "day").format("YYYY-MM-DD");
        tItem[key1] = dayjs(item[key]).add(i, "day").format("YYYY-MM-DD");
        results.push(tItem);
      }
    }
  }
  return results;
};

/**
 *@Author: QWP
 *@Description: 更具分组后的数据，计算没个分组数量
 *@Date 2024-09-11 20:10
 */
export const calcGroup = (data: any[], params: { key: string }): any[] => {
  let results: any[] = [];
  const { key } = params;
  data.forEach((item) => {
    let total = [0, ...item.map((o) => numberC(o[key]))].reduce((a, b) => addC(a, b));
    let tItem = item[0] || {};
    tItem[key] = total;
    results.push(tItem);
  });
  return results;
};
